package com.grom.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.dispacher.context.ContextHolder;
import com.grom.po.Brand;
import com.grom.po.Customer;
import com.grom.po.Store;
import com.grom.service.BrandService;
import com.grom.service.CustomerService;
import com.grom.service.GcmsUserService;
import com.grom.service.StoreService;

public class ExportAction {
	public void exportAllCustomer() {
		HttpServletResponse response = ContextHolder.findInstance()
				.getResponse();
		response.addHeader("Content-Disposition",
				"attachment; filename=customer.csv");
		List<Customer> allCustomer = CustomerService.instance.getAllCustomer();
		try {
			PrintWriter out = response.getWriter();
			out.append("客户分类,公司名称,公司地址,公司网站,是否安装客流统计,销售人员\r\n");
			for (Customer customer : allCustomer) {
				out.append(customer.getCusType() + ",");
				out.append(customer.getCustomerName() + ",");
				out.append(customer.getVisitedAddress() + ",");
				out.append(customer.getWebSite() + ",");
				out.append(customer.getEverUsedFlowCover() + ",");
				out.append(GcmsUserService.instance.getUserByID(
						customer.getUserId()).getUserName());
				out.append("\r\n");
			}
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void exportAllBrand() {
		HttpServletResponse response = ContextHolder.findInstance()
				.getResponse();
		response.addHeader("Content-Disposition",
				"attachment; filename=brand.csv");
		List<Brand> allBrands = BrandService.instance.getAllBrands();
		try {
			PrintWriter out = response.getWriter();
			out.append("品牌名称,品牌类型,品牌地址,品牌网站,收银系统型号,销售人员\r\n");
			for (Brand brand : allBrands) {
				out.append(brand.getBrandName() + ",");
				out.append(brand.getClothType() + ",");
				out.append(brand.getAddress() + ",");
				out.append(brand.getWebSite() + ",");
				out.append(brand.getChargerType() + ",");
				out.append(GcmsUserService.instance.getUserByID(
						brand.getUserId()).getUserName());
				out.append("\r\n");
			}
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void exportAllStore() {
		HttpServletResponse response = ContextHolder.findInstance()
				.getResponse();
		response.addHeader("Content-Disposition",
				"attachment; filename=store.csv");
		List<Store> allStores = StoreService.instance.getAllStore();
		try {
			PrintWriter out = response.getWriter();
			out.append("店铺地址,店铺负责人,店铺负责人联系电话,销售人员\r\n");
			for (Store store : allStores) {
				out.append(store.getAddresss() + ",");
				out.append(store.getStoreManeger() + ",");
				out.append(store.getPhoneOfStoreManager() + ",");
				out.append(GcmsUserService.instance.getUserByID(
						store.getUserId()).getUserName());
				out.append("\r\n");
			}
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
